package com.emar.adx.yiqifa.dao;

import com.emar.adx.yiqifa.entity.Feedback;
import com.emar.adx.yiqifa.util.JdbcUtil;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2015/11/17.
 */
//@Repository
public class FeedbackDao extends BaseSqlDAO{


    public int[] batchUpdate(String tableName,final List<Feedback> feedbackList) throws Exception{

        String sql = "INSERT INTO "+tableName+" (`FEED_BACK_KEY`, `feedbackValue`, `websiteId`, `pvId`, `ptype`, `spm`, " +
                "`linkId`, `clickIP`, `linkType`, `euId`, `userId`, `cTime`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?); ";

        int[] updateCounts = super.getJdbcTemplate().batchUpdate(
                sql,
                new BatchPreparedStatementSetter() {
                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        ps.setString(1, (feedbackList.get(i).getFeedbackKey()));
                        ps.setString(2, (feedbackList.get(i)).getFeedbackValue());
                        ps.setLong(3, (feedbackList.get(i).getWebsiteId()));
                        ps.setString(4, (feedbackList.get(i).getPvId()));
                        ps.setString(5, (feedbackList.get(i).getPtype()));
                        ps.setString(6, (feedbackList.get(i).getSpm()));
                        ps.setLong(7, (feedbackList.get(i).getLinkId()));
                        ps.setString(8, (feedbackList.get(i).getClickIP()));
                        ps.setString(9, (feedbackList.get(i).getLinkType()));
                        ps.setString(10, (feedbackList.get(i).getEuId()));
                        ps.setLong(11, (feedbackList.get(i).getUserId()));
                        ps.setLong(12, (feedbackList.get(i).getcTime()));
                    }

                    public int getBatchSize() {
                        return feedbackList.size();
                    }
                });
        return updateCounts;
    }


    public int getCount(String tableName){
        String sql = "select count(*) from "+tableName;
        return super.getCount(sql);
    }

    public List<Feedback> findFeedbackList(String tableName,Feedback feedback,int offset,int limit){
        String sql = "select f.`FEED_BACK_KEY` AS feedbackKey,f.`feedbackValue` from "+tableName;
        return super.getPageList(sql, Feedback.class, offset, limit);
    }

    public Feedback findOneByKey(String tableName,String key){
        StringBuffer sb = new StringBuffer("select f.`FEED_BACK_KEY` AS feedbackKey,f.`feedbackValue` from ");
        sb.append(tableName).append(" f ");
        sb.append(" where feed_back_key = '").append(key.trim()).append("'");
        return super.getJavaBean(sb.toString(),Feedback.class);
    }

}
